package com.woyou.ui.activity;

import java.util.List;
import java.util.Map;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.CircleOptions;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.citaq.ideliver.R;
import com.woyou.bean.Ladder;
import com.woyou.bean.Result;
import com.woyou.bean.rpc.QueryRadiusReq;
import com.woyou.utils.ParseUtils;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import retrofit.RetrofitError;

/**
 * 外送地图
 * 
 * @author
 *
 */
@EActivity
public class DeliveryMapActivity extends BaseActivity implements OnClickListener {
	
	private View view;
	@ViewById(R.id.back)
	RelativeLayout back;
	@ViewById(R.id.head_title)
	TextView headTitle;
	@ViewById(R.id.fm_deliverymap_layout1)
	RelativeLayout fm_deliverymap_layout1;
	@ViewById(R.id.fm_deliverymap_tv1)
	TextView fm_deliverymap_tv1;
	@ViewById(R.id.fm_deliverymap_layout2)
	RelativeLayout fm_deliverymap_layout2;
	@ViewById(R.id.fm_deliverymap_tv2)
	TextView fm_deliverymap_tv2;
	@ViewById(R.id.fm_deliverymap_layout3)
	RelativeLayout fm_deliverymap_layout3;
	@ViewById(R.id.fm_deliverymap_tv3)
	TextView fm_deliverymap_tv3;

	private String sId = "";
	private float lat = 0f;
	private float lng = 0f;
	private MapView mapView;
	private AMap aMap;
	List<Ladder> ladder;
	private LatLng shopLocation;

	@AfterViews
	public void initView() {
		headTitle.setText("地图与配送范围");
		back.setOnClickListener(this);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		view = View.inflate(mContext, R.layout.fm_deliverymap, null);
		setContentView(view);
		mapView = (MapView) view.findViewById(R.id.fm_deliverymap_map);
		mapView.onCreate(savedInstanceState);
		if (aMap == null) {
			aMap = mapView.getMap();
			if(hasInfo()){
				Object obj = mInfo.getData();
				if (obj instanceof Map) {
					Map<String, Object> map = (Map<String, Object>) obj;
					sId = (String) map.get("sId");
					lat = (Float) map.get("lat");
					lng = (Float) map.get("lng");
				}
			}
			queryRadius(sId);
		}
	}

	@UiThread
	public void setUpMap(List<Ladder> ladder) {
		aMap.clear();
		fm_deliverymap_layout1.setVisibility(View.GONE);
		fm_deliverymap_layout2.setVisibility(View.GONE);
		fm_deliverymap_layout3.setVisibility(View.GONE);
		shopLocation = new LatLng(lat, lng);
		aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(shopLocation, 15));// 设置指定的可视区域地图
		// 绘制一个圆形
		int size = ladder.size() - 1;
		for (int i = size; i >= 0; i--) {
			if (i == 0) {
				fm_deliverymap_layout1.setVisibility(View.VISIBLE);
				fm_deliverymap_tv1.setText("" + convertInfo(ladder.get(i)));
				aMap.addCircle(new CircleOptions().center(shopLocation).radius(ladder.get(i).getRadius())
						.fillColor(Color.argb(50, 60, 115, 255)).strokeWidth(1));
			} else if (i == 1) {
				fm_deliverymap_layout2.setVisibility(View.VISIBLE);
				fm_deliverymap_tv2.setText("" + convertInfo(ladder.get(i)));
				aMap.addCircle(new CircleOptions().center(shopLocation).radius(ladder.get(i).getRadius())
						.fillColor(Color.argb(50, 0, 67, 220)).strokeWidth(1));
			} else if (i == 2) {
				fm_deliverymap_layout3.setVisibility(View.VISIBLE);
				fm_deliverymap_tv3.setText("" + convertInfo(ladder.get(i)));
				aMap.addCircle(new CircleOptions().center(shopLocation).radius(ladder.get(i).getRadius())
						.fillColor(Color.argb(50, 180, 0, 255)).strokeWidth(1));
			}
		}
		drawMarkers();
	}

	/**
	 * 转换内容
	 */
	public String convertInfo(Ladder ladder) {
		String info = ParseUtils.String2Money(ladder.getSpending()) + "元起送  ";
		if (ladder.getSendFee() <= 0) {
			info = info + "免外送费";
		} else {
			info = info + ParseUtils.String2Money(ladder.getSendFee()) + "元外送费";
		}
		return info;
	}

	/**
	 * 绘制1种marker背景图片
	 */
	public void drawMarkers() {
		Marker marker = aMap.addMarker(new MarkerOptions().position(shopLocation)
				.icon(BitmapDescriptorFactory.fromResource(R.raw.map_location)));
		marker.showInfoWindow();// 设置默认显示一个infowinfow
	}

	/**
	 * 加载店铺营业范围数据
	 */
	@Background
	public void queryRadius(String sId) {
		QueryRadiusReq queryRadiusReq = new QueryRadiusReq();
		queryRadiusReq.setsId(sId);
		try {
			Result<List<Ladder>> result = mShopsModel.v2_1queryRadius(queryRadiusReq);
			if (result != null && result.data != null) {
				ladder = result.data;
				setUpMap(ladder);
			}
		} catch (RetrofitError e) {
			switch (e.getKind()) {
			case HTTP:
				break;
			case CONVERSION:
				break;
			case NETWORK:
				break;
			case UNEXPECTED:
				break;
			}
		}

	}

	@Override
	public void onResume() {
		super.onResume();
		mapView.onResume();
	}

	/**
	 * 方法必须重写 map的生命周期方法
	 */
	@Override
	public void onPause() {
		super.onPause();
		mapView.onPause();
	}

	/**
	 * 方法必须重写 map的生命周期方法
	 */
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mapView.onSaveInstanceState(outState);
	}

	/**
	 * 方法必须重写 map的生命周期方法
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
		mapView.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		}
	}

	@Override
	public boolean backOperate() {
		// TODO Auto-generated method stub
		return false;
	}

}
